package com.it.zuoning.sbh.file.service.impl;

import com.google.common.collect.Lists;
import com.it.zuoning.sbh.file.service.StoreFileService;
import com.it.zuoning.sbh.file.utils.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

/**
 * @ClassName StoreFileServiceImpl
 * @Description: 静态文件处理
 * @Author 小白
 * @Date 2019/12/15
 * @Version V1.0
 **/
@Component
public class StoreFileServiceImpl implements StoreFileService {

    private static final long serialVersionUID = 917660144738547668L;

    @Override
    public List<String> staticFileList(String filePath, HttpServletResponse response) {
        File rootFile = new File(StringUtils.isBlank(filePath) ? rootPath : rootPath + filePath);
        if (!rootFile.exists()) {
            return Lists.newArrayList();
        }
        if (rootFile.isFile()) {
            FileUtils.downloadFile(rootFile, response);
            return null;
        }
        return Arrays.asList(rootFile.list());
    }
}
